package io.chance.receiverlei.db;

import lrq.data.Model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据池，为了提升性能，把接收到的数据缓存起来，达到一定数据量后再入库
 * @author gislin
 * 2016年3月2日
 */
public class DataPool 
{
	private List<Model> modelPool = new ArrayList<>();
	private boolean isExecuting = false;
	private String id; // 数据缓冲池id
	private Date newDataStartTime;// 时间戳，处理长时间都没有达到缓存最大值的情况
	
	public DataPool(String id){
		this.id = id;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public <T extends Model> void addModel(List<T> modelList) {
		if(newDataStartTime == null){
			newDataStartTime = new Date();
		}
		modelPool.addAll(modelList);
	}
	
	public int getModelPoolSize() {
		return modelPool.size();
	}
	
	public List<Model> getModelPool() {
		return modelPool;
	}

	public boolean isExecuting() {
		return isExecuting;
	}

	public void setExecuting(boolean isExecuting) {
		this.isExecuting = isExecuting;
	}
	
	public void clear(){
		modelPool.clear();
		newDataStartTime = null;
	}

	public long getNewDataStartTimeDiff() {
		if(newDataStartTime == null){
			return 0;
		}
		long timeDiff = ((new Date()).getTime() - newDataStartTime.getTime()) / 1000;
		return timeDiff;
	}
}